package com.fc.commons;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fc.commons.logger.UtilLogger;

public final class JsonUtils {

	/**
	 * @return
	 */
	public static byte[] object2Bytes(Object object) {
		try {
			return JSON.toJSONBytes(object);

		} catch (Exception e) {

			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @return
	 */
	public static byte[] object2BytesNoRef(Object object) {
		try {
			return JSON.toJSONBytes(object,
					SerializerFeature.DisableCircularReferenceDetect);

		} catch (Exception e) {

			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @return
	 */
	public static byte[] object2BytesNoRef(Object object, SerializeFilter filter) {
		try {
			if (object == null)
				return null;
			String jsonString = JSON.toJSONString(object, filter,
					SerializerFeature.DisableCircularReferenceDetect);
			return jsonString.getBytes();

		} catch (Exception e) {

			e.printStackTrace();
			return null;
		}
	}

	public static String object2String(Object object) {
		try {
			return JSON.toJSONString(object);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static <T> String object2String(Object obj, SerializeFilter filter) {
		return JSON.toJSONString(obj, filter);
	}

	/**
	 * @param object
	 * @return
	 */
	public static String object2StringNoRef(Object object) {
		try {
			return JSON.toJSONString(object,
					SerializerFeature.DisableCircularReferenceDetect);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @param object
	 * @return
	 */
	public static String object2StringNoRef(Object object,
			SerializeFilter filter) {
		try {
			return JSON.toJSONString(object, filter,
					SerializerFeature.DisableCircularReferenceDetect);
		} catch (Exception e) {
			UtilLogger.instance().getLogger().error(e.getMessage(),e);
			return null;
		}
	}

	public static <T> T bytes2Object(byte[] bytes, Class<T> type) {
		try {
			return JSON.parseObject(bytes, type);

		} catch (Exception e) {
			UtilLogger.instance().getLogger().error(e.getMessage(),e);
			return null;
		}
	}

	public static <T> T string2Object(String content, Class<T> clz) {
		try {
			return JSON.parseObject(content, clz);
		} catch (Exception e) {
			UtilLogger.instance().getLogger().error(e.getMessage(),e);
			return null;
		}
	}

	public static <T> T string2Object(String content, TypeReference<T> type) {
		try {
			return JSON.parseObject(content, type);
		} catch (Exception e) {
			UtilLogger.instance().getLogger().error(e.getMessage(),e);
			return null;
		}
	}

	public static <T> List<T> string2List(String content, Class<T> clz) {
		try {
			return JSON.parseArray(content, clz);
		} catch (Exception e) {
			UtilLogger.instance().getLogger().error(e.getMessage(),e);
			return null;
		}
	}

	public static JSONObject string2JsonObject(String content) {
		try {
			return JSON.parseObject(content);
		} catch (Exception e) {
			UtilLogger.instance().getLogger().error(e.getMessage(),e);
			return null;
		}
	}

	public static JSONObject bytes2JsonObject(byte[] bytes) {
		try {
			return JSON.parseObject(bytes, JSONObject.class);
		} catch (Exception e) {
			UtilLogger.instance().getLogger().error(e.getMessage(),e);
			return null;
		}
	}
}
